/**
 * 
 */
package cn.haona.game.simb.object.team;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.haona.game.simb.comm.util.Util;
import cn.haona.game.simb.object.TeamImpl;
import cn.haona.game.simb.object.bio.Fighter;
import cn.haona.game.simb.object.bio.Human;
import cn.haona.game.simb.object.department.HAB;
import cn.haona.game.simb.object.god.MainManager;

/**
 * 打斗团队战斗者原型
 * 
 * @author shenaho
 * 
 */
public class FightTeamFighterOrg extends TeamImpl implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2455264495148082188L;

	// 团队领导者
	private Long leaderID;

	// 领导类型
	private int leaderType;

	// NPC队员
	private List<Long> listNpcID;

	// USER队员
	private List<Long> listUserID;

	// 团队类型
	final public static int TEAM_TYPE_NPC = 0;
	final public static int TEAM_TYPE_USER = 1;
	final public static int TEAM_TYPE_NPCUSER = 2;
	final public static int TEAM_TYPE_USERNPC = 3;

	/**
	 * 
	 */
	public FightTeamFighterOrg()
	{
		super();
		listNpcID = new ArrayList<Long>();
		listUserID = new ArrayList<Long>();
	}

	/**
	 * 得到团队原型
	 * 
	 * @param sponsor
	 * @param fighterNum
	 * @param teamType
	 * @return
	 */
	public static FightTeamFighterOrg getFightTempTeam(Long sponsor, int fighterNum, int teamType)
	{
		FightTeamFighterOrg ftt = null;
		switch (teamType)
		{
		// 纯NPC
		case TEAM_TYPE_NPC:
			ftt = getFightTempTeamNpc(sponsor, fighterNum);
			break;

		// 纯User
		case TEAM_TYPE_USER:
			ftt = getFightTempTeamUser(sponsor, fighterNum);
			break;

		// NPC头目+杂兵
		case TEAM_TYPE_NPCUSER:
			ftt = getFightTempTeamNpcUser(sponsor, fighterNum);
			break;

		// User头目+杂兵
		case TEAM_TYPE_USERNPC:
			ftt = getFightTempTeamUserNpc(sponsor, fighterNum);
			break;

		default:
			ftt = null;

		}
		return ftt;
	}

	/**
	 * 纯NPC团队
	 * 
	 * @param sponsor
	 * @param fighterNum
	 * @return
	 */
	private static FightTeamFighterOrg getFightTempTeamNpc(Long sponsor, int fighterNum)
	{
		FightTeamFighterOrg ftfo = new FightTeamFighterOrg();
		// 寻找一个非保镖的NPC为头目
		Long lLeaderID = getNpcLeaderID(null, new int[]
		{ Human.JOB_BODYGUARD_LEVEL_0 });
		// 设置头目
		ftfo.setLeaderID(lLeaderID);
		// 头目类型USER OR NPC
		ftfo.setLeaderType(Human.HUMAN_TYPE_NPC);
		// 计算队员人数
		int iMemberNum = fighterNum - 1;
		if (iMemberNum > 0)
		{
			// 设置队员全部为NPC
			ftfo.setListNpcID(getNpcMembers(lLeaderID, iMemberNum));
		}
		return ftfo;
	}

	/**
	 * 纯玩家团队
	 * 
	 * @param sponsor
	 * @param fighterNum
	 * @return
	 */
	private static FightTeamFighterOrg getFightTempTeamUser(Long sponsor, int fighterNum)
	{
		FightTeamFighterOrg ftfo = new FightTeamFighterOrg();
		// 寻找一个非自己的User为头目
		Long lLeaderID = getUserLeaderID(sponsor);
		ftfo.setLeaderID(lLeaderID);
		// 头目类型USER OR NPC
		ftfo.setLeaderType(Human.HUMAN_TYPE_USER);
		// 计算队员人数
		int iMemberNum = fighterNum - 1;
		if (iMemberNum > 0)
		{
			// 设置队员全部为User
			ftfo.setListUserID(getUserMembers(sponsor, lLeaderID, iMemberNum));
		}
		return ftfo;
	}

	/**
	 * NPC为头目，玩家+NPC为队员
	 * 
	 * @param sponsor
	 * @param fighterNum
	 * @return
	 */
	private static FightTeamFighterOrg getFightTempTeamNpcUser(Long sponsor, int fighterNum)
	{
		FightTeamFighterOrg ftfo = new FightTeamFighterOrg();
		Long lLeaderID = getNpcLeaderID(null, new int[]
		{ Human.JOB_BODYGUARD_LEVEL_0 });
		ftfo.setLeaderID(lLeaderID);
		// 头目类型USER OR NPC
		ftfo.setLeaderType(Human.HUMAN_TYPE_NPC);
		int iMemberNum = fighterNum - 1;
		// 随机创建队员
		ftfo = randomCreateMembers(ftfo, sponsor, lLeaderID, iMemberNum,Human.HUMAN_TYPE_NPC);

		return ftfo;
	}

	/**
	 * 玩家为头目，玩家+NPC为队员
	 * 
	 * @param sponsor
	 * @param fighterNum
	 * @return
	 */
	private static FightTeamFighterOrg getFightTempTeamUserNpc(Long sponsor, int fighterNum)
	{
		FightTeamFighterOrg ftfo = new FightTeamFighterOrg();
		Long lLeaderID = getUserLeaderID(sponsor);
		ftfo.setLeaderID(lLeaderID);
		// 头目类型USER OR NPC
		ftfo.setLeaderType(Human.HUMAN_TYPE_USER);
		int iMemberNum = fighterNum - 1;
		// 随机创建队员
		ftfo = randomCreateMembers(ftfo, sponsor, lLeaderID, iMemberNum,Human.HUMAN_TYPE_USER);

		return ftfo;
	}

	/**
	 * 随机创建队员
	 * 
	 * @param ftfo
	 * @param sponsor
	 * @param lLeaderID
	 * @param iMemberNum
	 * @return
	 */
	private static FightTeamFighterOrg randomCreateMembers(FightTeamFighterOrg ftfo, Long sponsor, Long lLeaderID,
			int iMemberNum, int leaderType)
	{

		// 如果可以有团队
		if (iMemberNum > 0)
		{
			// 看剩下的团队可否一分为二
			// 可以拆分
			if (iMemberNum >= 2)
			{
				int iUserNum = iMemberNum / 2;
				int iNpcNum = iMemberNum - iUserNum;
				ftfo.setListUserID(getUserMembers(sponsor, lLeaderID, iUserNum));
				// 如果是玩家做领导,那么就没有NPC领导
				if (leaderType == Human.HUMAN_TYPE_USER)
				{
					lLeaderID = -1L;
				}
				ftfo.setListNpcID(getNpcMembers(lLeaderID, iNpcNum));
			}
			// 不能拆分了
			else
			{
				// 真---添加一玩家
				if (Util.randomTrueFalse())
				{
					ftfo.setListUserID(getUserMembers(sponsor, lLeaderID, iMemberNum));
				}
				// 假---添加一NPC
				else
				{
					// 如果是玩家做领导,那么就没有NPC领导
					if (leaderType == Human.HUMAN_TYPE_USER)
					{
						lLeaderID = -1L;
					}
					ftfo.setListNpcID(getNpcMembers(lLeaderID, iMemberNum));
				}
			}
		}// end if(如果可以有团队)

		return ftfo;
	}

	@Override
	/*
	 * 销毁我
	 */
	public void destroyMe()
	{
		// TODO Auto-generated method stub
		super.destroyMe();
		listNpcID.clear();
		listUserID.clear();
		listNpcID = null;
		listUserID = null;
	}

	public Long getLeaderID()
	{
		return leaderID;
	}

	public void setLeaderID(Long leaderID)
	{
		this.leaderID = leaderID;
	}

	public List<Long> getListNpcID()
	{
		return listNpcID;
	}

	public void setListNpcID(List<Long> listNpcID)
	{
		this.listNpcID = listNpcID;
	}

	public List<Long> getListUserID()
	{
		return listUserID;
	}

	public void setListUserID(List<Long> listUserID)
	{
		this.listUserID = listUserID;
	}

	public int getLeaderType()
	{
		return leaderType;
	}

	public void setLeaderType(int leaderType)
	{
		this.leaderType = leaderType;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.haona.game.simb.object.Cell#initPO()
	 */
	public void initPO()
	{
		// TODO Auto-generated method stub

	}

}
